home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / arts / audiosubsys.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  6.1 KB  |  237 lines

  1.     /*
  2.  
  3.     Copyright (C) 2000 Stefan Westerfeld
  4.                        stefan@space.twc.de
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.   
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.    
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.     Boston, MA 02111-1307, USA.
  20.  
  21.     */
  22.  
  23. #ifndef AUDIOSUBSYS_H
  24. #define AUDIOSUBSYS_H
  25. #include <string>
  26.  
  27. #include "arts_export.h"
  28. #include "pipebuffer.h"
  29. #include "startupmanager.h"
  30.  
  31. /*
  32.  * BC - Status (2002-03-08): AudioSubSystem, ASProducer, ASConsumer.
  33.  *
  34.  * These classes are kept binary compatible. You can rely on them.
  35.  * AudioSubSystem has a private data pointer to do so. Even if ports to
  36.  * other architectures or hardware will require different organization
  37.  * (i.e. no fragments, direct memory access via mmap), the data members
  38.  * and functions exported here MAY NOT BE CHANGED. Use the private data
  39.  * pointer for adding data members.
  40.  *
  41.  * If ASProducer/ASConsumer doesn't suit the needs any longer, do NOT
  42.  * change them. Add new classes instead.
  43.  */
  44.  
  45. namespace Arts {
  46.  
  47. class ASProducer {
  48. public:
  49.     virtual void needMore() = 0;
  50. };
  51.  
  52. // FIXME: haveMore won't be called currently
  53. class ASConsumer {
  54. public:
  55.     virtual void haveMore() = 0;
  56. };
  57.  
  58. class ARTS_EXPORT AudioSubSystemStart :public StartupClass {
  59. protected:
  60.     class AudioSubSystem *_instance;
  61. public:
  62.     inline AudioSubSystem *the() { return _instance; };
  63.     void startup();
  64.     void shutdown();
  65. };
  66.  
  67. class AudioSubSystemPrivate;
  68.  
  69. class ARTS_EXPORT AudioSubSystem {
  70.     class AudioSubSystemPrivate *d;
  71.  
  72.     std::string _error;
  73.     char *fragment_buffer;
  74.  
  75.     int _fragmentCount;
  76.     int _fragmentSize;
  77.  
  78.     bool _running;
  79.     PipeBuffer wBuffer, rBuffer;
  80.     ASConsumer *consumer;
  81.     ASProducer *producer;
  82.  
  83.     friend class AudioSubSystemStart;
  84.     AudioSubSystem();
  85.     ~AudioSubSystem();
  86.  
  87.     void close();
  88.     void initAudioIO();
  89.  
  90.     /**
  91.      * checks that full duplex doesn't go out of sync
  92.      */
  93.     void adjustDuplexBuffers();
  94.  
  95.     /**
  96.      * creates count empty fragments of input in rBuffer if count > 0
  97.      * removes count fragments of input from rBuffer if count < 0
  98.      */
  99.     void adjustInputBuffer(int count);
  100.  
  101. public:
  102.     enum { ioRead=1, ioWrite=2, ioExcept=4 };
  103.  
  104.     // singleton
  105.     static AudioSubSystem *the();
  106.  
  107.     /*
  108.      * Currently, if you want to use the AudioSubSystem, you need to
  109.      * 
  110.      * 1. - attach one producer
  111.      *    - attach one consumer (only for full duplex)
  112.      *    - open the audio subsystem using open (watch the fd)
  113.      *    (in any order)
  114.      *
  115.      * 2. react on the callbacks you get for the producer
  116.      *
  117.      * 3. if you don't need the audio subsystem any longer, call detach
  118.      *    both, the producer and the cosumer.
  119.      *
  120.      * Be careful that you don't read/write from/to the audio subsystem
  121.      * when running() is not true.
  122.      */
  123.  
  124.     bool attachProducer(ASProducer *producer);
  125.     bool attachConsumer(ASConsumer *consumer);
  126.  
  127.     void detachProducer();
  128.     void detachConsumer();
  129.  
  130.     /*
  131.      * can be used to select the AudioIO class to use, reasonable choices
  132.      * may be "oss" or "alsa" at this point in time - you need to choose
  133.      * this before doing anything else
  134.      */
  135.     void audioIO(const std::string& audioIO);
  136.     std::string audioIO();
  137.  
  138.     // which device to use for audio output (default /dev/dsp)
  139.     void deviceName(const std::string& deviceName);
  140.     std::string deviceName();
  141.  
  142.     void fragmentSize(int size);
  143.     int fragmentSize();
  144.  
  145.     void fragmentCount(int fragments);
  146.     int fragmentCount();
  147.  
  148.     void samplingRate(int samplingrate);
  149.     int samplingRate();
  150.  
  151.     void channels(int channels);
  152.     int channels();
  153.  
  154.     void format(int format);
  155.     int format();
  156.  
  157.     /**
  158.      * As opposed to format(), this one returns the number of bits used per
  159.      * sample. Thats sometimes a difference, for instance 16bit big endian
  160.      * encoding has the format() 17, whereas bits() would return 16.
  161.      */
  162.     int bits();
  163.  
  164.     void fullDuplex(bool newFullDuplex);
  165.     bool fullDuplex();
  166.  
  167.     bool check();
  168.  
  169.     /**
  170.      * Opens the audio device.
  171.      *
  172.      * After opening, you must check selectReadFD() and selectWriteFD() to
  173.      * select() on the appropriate file descriptors. Whenever select()ing is
  174.      * successful, handleIO needs to be called.
  175.      *
  176.      * The type for handleIO must be set to ioRead if fd is ready for
  177.      * reading, ioWrite if fd is ready for writing, ioExcept if something
  178.      * special happend or any combination of these using bitwise or.
  179.      *
  180.      * @returns true if audio device has been opened successfully,
  181.      *          false otherwise
  182.      */
  183.     bool open();
  184.  
  185.     /**
  186.      * human readable error message that descibes why opening the audio device
  187.      * failed
  188.      */
  189.     const char *error();
  190.  
  191.     /**
  192.      * File descriptor to select on for reading (@see open()), -1 if there is
  193.      * none.
  194.      */
  195.     int selectReadFD();
  196.  
  197.     /**
  198.      * File descriptor to select on for writing (@see open()), -1 if there is
  199.      * none.
  200.      */
  201.     int selectWriteFD();
  202.  
  203.     /**
  204.      * Needs to be called to handle I/O on the filedescriptors given by
  205.      * selectReadFD() and selectWriteFD() (@see open()).
  206.      */
  207.     void handleIO(int type);
  208.  
  209.     void read(void *buffer, int size);
  210.     void write(void *buffer, int size);
  211.  
  212.     /**
  213.      * this returns the time in seconds it will take until everything written
  214.      * to the AudioSubSystem so far will be played - in other words, it returns
  215.      * the time it will take until the next sample written by the application
  216.      * will be heard by the user
  217.      */
  218.     float outputDelay();
  219.  
  220.     /**
  221.      * returns true as long as the audio subsystem is opened and active (that
  222.      * is, between successful opening, with attaching producer, and the first
  223.      * detachConsumer/detachProducer)
  224.      */
  225.     bool running();
  226.  
  227.     /**
  228.      * called during crashes, to release the opened device (and other resources)
  229.      * don't use this method for other purposes than emergencies
  230.      */
  231.     void emergencyCleanup();
  232. };
  233.  
  234. }
  235.  
  236. #endif /* AUDIOSUBSYS_H */
  237.